package Graph;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GraphPanel extends JPanel {

    private final int DISTANCE = 5, DELAY = 25, HEIGHT = 150;
    private Timer timer = new Timer(DELAY, new TimeListener());
    private int werte[] = new int[90], amount;

    public GraphPanel() {
        this.setBackground(Color.lightGray);
        werte[0] = 100;
        timer.start();
        amount = 0;
    }

    public void paintComponent(Graphics page) {
        page.setColor(Color.red);
        for (int i = 1; i < werte.length; i++) {
            if (werte[i] != 0) {
                page.drawLine((i - 1) * DISTANCE, HEIGHT - werte[i - 1], i * DISTANCE, HEIGHT - werte[i]);
            }

        }
        if (amount >= werte.length - 1) {
            page.setColor(Color.lightGray);
            page.fillRect(0, 0, 500, 200);
        }
    }

    public class TimeListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            werte[amount] = (int) (Math.random() * 100 + 45);
            if (amount >= werte.length - 1) {
                amount = 0;

            }
            amount++;
            repaint();
        }
    }

    public void addValue(int budget) {

        werte[amount] = budget;
        amount++;
        repaint();

    }
}
